package erp.server.order.service.impl;

import erp.server.base.exception.ServiceException;
import erp.server.base.response.StatusCode;
import erp.server.order.entity.dto.UseResource;
import erp.server.order.entity.pojo.Order;
import erp.server.order.entity.vo.OrderVO;
import erp.server.order.mapper.OrderMapper;
import erp.server.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    OrderMapper orderMapper;

    @Override
    public void use(UseResource useResource) {
        Order order = new Order();
        BeanUtils.copyProperties(useResource,order);
        if (order.getStartTime() == null){
            order.setStartTime(new Date());
        }
        order.setOrderTime(new Date());
        int result = orderMapper.use(order);
        if (result == 0){
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        }
    }

    @Override
    public List<OrderVO> selectByUserID(String userID){
        return orderMapper.selectOrderList(userID);
    }

    @Override
    public OrderVO selectByOrderID(String orderID){
        return orderMapper.selectOrder(orderID);
    }
}
